package softtech.apps.callblocker.adapter;

import java.util.ArrayList;
import java.util.List;

import softtech.apps.utility.FileUtility;
import softtech.apps.callblocker.R;
import softtech.apps.callblocker.model.BlackListItem;
import softtech.apps.callblocker.model.DatabaseHanler;
import softtech.apps.callblocker.model.HeaderListViewStruct;
import softtech.apps.callblocker.model.SmsBlockedItem;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class AdapterChatSms extends ArrayAdapter<SmsBlockedItem>{
	
	List<SmsBlockedItem> mListSms;
	Context mContext;
	DatabaseHanler db;
	private BlackListItem mContact;
	List<HeaderListViewStruct> listHeader = null;
	
	public AdapterChatSms(Context context , int textViewResourceId , ArrayList<SmsBlockedItem> list){
		super(context, textViewResourceId);
		db = new DatabaseHanler(context);
		mContext = context;
		mListSms = list;
		initListHeader();
	}
	
	@Override
	public int getCount() {
		if(mListSms==null || mListSms.equals(null) || mListSms.size() == 0){
			return 1;
		}
		return mListSms.size();
	}

	@Override
	public SmsBlockedItem getItem(int position) {
		if(mListSms==null || mListSms.equals(null) || mListSms.size() == 0){
			return null;
		}
		return mListSms.get(position);
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		if(parent.getContext()==null){		
			return null;
		}
		
		if(mListSms==null || mListSms.equals(null) || mListSms.size()==0){
			LayoutInflater inflater = LayoutInflater.from(parent.getContext());
			convertView = inflater.inflate(R.layout.item_search_notfound,parent, false);
			return convertView;
		}
		final SmsBlockedItem currentItem = mListSms.get(position);
		LayoutInflater inflater = LayoutInflater.from(parent.getContext());
		
		int isIndex = -1;
		
		for(int i = 0 ; i < listHeader.size(); i++  ){
			if(listHeader.get(i).getPosition() == position ){
				isIndex = listHeader.get(i).getPosition();
			}
		}
		
		if(isIndex!=-1){
			if(currentItem.getStatus() == SmsBlockedItem.SEND){
				convertView = inflater.inflate(R.layout.item_sms_mine_header,parent, false);
			}else{
				convertView = inflater.inflate(R.layout.item_sms_their_header,parent, false);
			}
			String title = "";
			try{
				title = listHeader.get(isIndex).getTitle();
				title = title.substring(0 , 11);
			}catch(Exception e){
				
			}
			
			((TextView)convertView.findViewById(R.id.tvDateHeader)).setText(title);
			
		}else{
			if(currentItem.getStatus() == SmsBlockedItem.SEND){
				convertView = inflater.inflate(R.layout.item_sms_mine,parent, false);
			}else{
				convertView = inflater.inflate(R.layout.item_sms_their,parent, false);
			}
		}
		
		
		
		((TextView)convertView.findViewById(R.id.tvContentSms)).setText(currentItem.getContent());
		if(currentItem.getStatus() == SmsBlockedItem.SEND){
			((TextView)convertView.findViewById(R.id.tvName)).setText("Me");
		}else{
			((TextView)convertView.findViewById(R.id.tvName)).setText(currentItem.getPhoneNumber());
		}
		
		try{
			((TextView)convertView.findViewById(R.id.tvTime)).setText(currentItem.getDate_time().substring(12, currentItem.getDate_time().length()));
		}catch(Exception e){
			
		}
		
		
		
		return convertView;
	}

	static class ViewHolder{
		TextView tvContentSms , tvName , tvTime;
	}

	public BlackListItem getContact() {
		return mContact;
	}

	public void setContact(BlackListItem mContact) {
		this.mContact = mContact;
	}

	public void initListHeader(){
		if(mListSms== null || mListSms.equals(null) || mListSms.size() == 0){
			return;
		}
		listHeader = new ArrayList<HeaderListViewStruct>();
		
		String last_date = mListSms.get(0).getDate_time().substring(0, 11);
		int last_position = 0;
		HeaderListViewStruct item = new HeaderListViewStruct(last_date, last_position);
		listHeader.add(item);
		for(int i =0 ; i < mListSms.size() ; i++){
			if(! last_date.contentEquals(mListSms.get(i).getDate_time().substring(0, 11))){
				last_position = i;
				last_date = mListSms.get(i).getDate_time().substring(0, 11);
				item = new HeaderListViewStruct(last_date, last_position);
				listHeader.add(item);
			}
		}
		
	}
	
}
